package com.lingnan.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.lingnan.pojo.ClassSchedule;
import com.lingnan.pojo.User;
import com.lingnan.service.ClassScheduleService;
import com.lingnan.util.HttpRequestUtil;
import com.lingnan.util.Result;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.UserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 * 课表 前端控制器
 * </p>
 *
 * @author 梁兴达
 * @since 2022-03-06
 */
@RestController
@CrossOrigin
@RequestMapping("/class-schedule")
public class ClassScheduleController {

    @Autowired
    ClassScheduleService classScheduleService;

    /**
     * 根据用户id展示课表
     * @param
     * @return
     */
    @PostMapping("/classList")
    @ApiOperation("根据用户id展示课表")
    public Result classList(@ApiParam("传学生no 你可以在list里面获取每一课表根据他的星期几 0-6 去显示在哪一个星期几")@RequestBody UserVo userVo) {
        //判断session里面是否这个学生登录
        QueryWrapper<ClassSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("no", userVo.getNo());
        List<ClassSchedule> list = classScheduleService.list(queryWrapper);
            if (!ObjectUtils.isEmpty(list)) {
                return ResultGenerator.genSuccessResult(200, list, "课表展示成功");
            }
            return ResultGenerator.genErrorResult(201, "课表展示失败");
        }

    /**
     * 添加课程
     * @param classSchedule
     * @return
     */
    @PostMapping("/changeClass")
    @ApiOperation("添加课程")
    public Result changeClass(@ApiParam("传课程对象顺便传星期几的0-6数字 字段叫：week")@RequestBody ClassSchedule classSchedule){
        if (!ObjectUtils.isEmpty(classSchedule)) {
            UpdateWrapper<ClassSchedule> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("week",classSchedule.getWeek())
            .eq("no",classSchedule.getNo());
            //System.out.println(classSchedule.getTime());
            boolean update = classScheduleService.saveOrUpdate(classSchedule, updateWrapper);
            if (update){
                return ResultGenerator.genSuccessResult(200, "添加课程成功");
            }
            return ResultGenerator.genErrorResult(201, "添加课程失败");

        }
        return ResultGenerator.genErrorResult(201, "添加课程失败");
    }


    /**
     * 修改课程
     * @param classSchedule
     * @return
     */
    @PostMapping("/updateClass")
    @ApiOperation("修改课程")
    public Result updateClass(@ApiParam("传课程对象，修改传课表id回来")@RequestBody ClassSchedule classSchedule){
        if (!ObjectUtils.isEmpty(classSchedule)) {
            UpdateWrapper<ClassSchedule> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("week", classSchedule.getWeek())
                    .eq("no",classSchedule.getNo())
                    .eq("id",classSchedule.getId());
            boolean update = classScheduleService.saveOrUpdate(classSchedule, updateWrapper);
            if (update){
                return ResultGenerator.genSuccessResult(200, "修改课程成功");
            }
            return ResultGenerator.genErrorResult(201, "修改课程失败");

        }
        return ResultGenerator.genErrorResult(201, "修改课程失败");
    }
}

